package com.shuyun.xxx.guava.offer;

import org.junit.Test;

import java.util.Stack;

/**
 * Created by goodb on 2017/5/21.
 * 一个栈依次压入1,2,3,4,5 栈顶到栈底为5,4,3,2,1 转置后栈顶到栈底为1,2,3,4,5
 */
public class ReverseStack {
    //1,2,3 ----> 1,2    return 3
    public static int getAndRemoveLastElement(Stack<Integer> stack){
        int result = stack.pop();
        if(stack.isEmpty()){
            return result;
        }else{
            int last = getAndRemoveLastElement(stack);
            stack.push(result);
            return last;
        }
    }

    public static void reverse(Stack<Integer> stack){
        if(stack.isEmpty()){
            return;
        }
        int i = getAndRemoveLastElement(stack);
        reverse(stack);
        stack.push(i);
    }
    @Test
    public void run(){
        Stack<Integer> stack = new Stack<>();
        stack.add(5);
        stack.add(4);
        stack.add(3);
        stack.add(2);
        stack.add(1);
        System.out.println(stack);
        reverse(stack);
        System.out.println(stack);
    }

}
